package com.mypage.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.litesuits.android.log.Log;
import com.mypage.model.NconvertClient;
import com.mypage.model.NewcontactLixnxi;
import com.mypage.view.activity.NewContactLianXi;
import com.mypage.view.activity.beautMore.ClientListActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuyongjie on 2017/5/11.
 */

public class NconvertClientAdapter extends BaseAdapter {

    public List<NconvertClient.Custom> mList = new ArrayList<>();
    public Context context;
    public String search;

    public NconvertClientAdapter(List<NconvertClient.Custom> mQList, Context context, String search) {
        this.mList.addAll(mQList);
        this.context = context;
        this.search = search;
    }

    public void changeData(List<NconvertClient.Custom> data) {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
        mList.addAll(data);
        Log.d("isnull", "is:" + mList != null + "::" + data.toString() + "::"
                + (data != null));
        notifyDataSetChanged();
    }

    /**
     * 添加数据集
     *
     * @param data
     */
    public void addData(List<NconvertClient.Custom> data) {
        if (mList == null) {
            mList = new ArrayList<NconvertClient.Custom>();
        }
        mList.addAll(data);
        notifyDataSetChanged();
        Log.d("istrue", "过来了");
    }

    @Override
    public int getCount() {
        return mList.size() == 0 ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.aclient_list_item, null);
            viewHolder.customName = (TextView) convertView.findViewById(R.id.customName);
            viewHolder.customPhone = (TextView) convertView.findViewById(R.id.customPhone);
            viewHolder.LinearLayout = (LinearLayout) convertView.findViewById(R.id.ll_item);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.LinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sName = mList.get(position).name;
                String mIds = mList.get(position).id;
                String kehu_name = mList.get(position).createdate;
                String kehu_id = mList.get(position).phone;
                Intent intent = new Intent();
                intent.putExtra("mName", sName);
                intent.putExtra("mId", mIds);
                intent.putExtra("kehu_name", kehu_name);
                intent.putExtra("kehu_id", kehu_id);
                ClientListActivity.instance.setResult(11, intent);
                ClientListActivity.instance.finish();
            }
        });

        if (mList.get(position).name!=null&&!"".equals(mList.get(position).name)){
            viewHolder.customName.setText(mList.get(position).name.toString().trim());

        }
        if (mList.get(position).phone!=null&&!"".equals(mList.get(position).phone)){
            viewHolder.customPhone.setText(mList.get(position).phone.toString());
        }




        return convertView;
    }

    public class ViewHolder {
        TextView customName, customPhone;
        LinearLayout LinearLayout;
    }
}
